'''
Created on 05.03.2011

@author: bstar
'''
import json
import logging
from lxml import etree
from dopplershift.core.containers.mediator import getMediator
from dopplershift.collector.testing.tests.calling import CallTestItem

logger = logging.getLogger("CALL")
class TestCollection:
    def __init__(self):
        self._collection = []
        config = getMediator().get("CollectorConfig")
        for el in config.get_calls_tests():
            parent = el.getparent()
            test = CallTestItem(
                                parent.get("id"),
                                config.get_upstream(parent.get("upstream")),
                                parent.get("service"),
                                self._loadDataFromNode(el, "request"),
                                self._loadDataFromNode(el, "params"),
                                self._loadDataFromNode(el, "cookies"),
                                method=el.get("method"),
                                auth=bool(el.get("auth")),
                                return_data=self._loadXMLFromNode(el, 'response')
                                )
            self._collection.append(test)
    def _loadXMLFromNode(self, node, node_tag):
        n = node.find(node_tag)
        if n is None: return None
        return etree.tostring(n).strip()
    def _loadDataFromNode(self, node, node_tag):
        n = node.find(node_tag)
        if n is None: return {}
        return json.loads(n.text)
    def getNextCallTest(self):
        if len(self._collection) > 0:
            return self._collection.pop()
            
        
